package com.sxu.system.controller.sysuser;

import com.sxu.common.core.constants.HttpConstants;
import com.sxu.common.core.controller.BaseController;
import com.sxu.common.core.domain.R;
import com.sxu.common.core.domain.vo.LoginUserVO;
import com.sxu.system.domain.sysuser.dto.LoginDTO;
import com.sxu.system.domain.sysuser.dto.SysUserSaveDTO;
import com.sxu.system.domain.sysuser.vo.SysUserVO;
import com.sxu.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

@RestController
@RequestMapping("/sysUser")
@Tag(name = "管理员接口") //给接口分组
public class SysUserController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    //返回值告知两个信息 成功/失败（boolean / int） 原因（）
    //请求方法

    /**
     * 管理员登录
     * 请求方法 ：post
     * 请求参数
     *
     * 接口地址 /sysUser/login
     * swagger生成接口文档
     */
    @PostMapping("/login")//考虑 安全性
    @Operation(summary = "管理员登陆",description = "根据账号密码进行管理员登陆") //描述接口
    @ApiResponse(responseCode = "1000",description = "登录成功") //描述返回值
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3102",description = "用户不存在")
    @ApiResponse(responseCode = "3103",description = "账号或密码错误")
    public R<String> login(@RequestBody LoginDTO loginDTO) {
        return sysUserService.login(loginDTO.getUserAccount(), loginDTO.getPassword());
    }

    //接口地址：/system/sysUser/logout
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(sysUserService.logout(token));
//        if (logout) {
//            return R.ok();
//        }
//        return R.fail();
    }

    //接口地址：/system/sysUser/info
    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return sysUserService.info(token);
    }

    //管理员的增删改查

    /**
     * 新增管理员
     * 接口地址：/sysUser/add
     * 请求方法：post
     * 请求参数：
     * 响应数据：
     */
    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员") //描述接口
    @ApiResponse(responseCode = "1000",description = "操作成功") //描述返回值
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已存在")
    public R<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO) {
        return toR(sysUserService.add(sysUserSaveDTO));
    }

    /**
     * 删除管理员
     * 接口地址：
     */
    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户", description = "通过用户Id删除用户")
    @Parameters(value = {
        @Parameter(name = "userId", in = ParameterIn.PATH, description = "用户ID")
                })
    @ApiResponse(responseCode = "1000", description = "成功删除用户")
    @ApiResponse(responseCode = "2000", description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户不存在")
    public R<Void> delete(@PathVariable Long userId) {
        return null;
    }

    /**
     * 查询管理员
     * 接口地址：
     */
    @Operation(summary = "用户详情", description = "根据查询条件查询用户详情")
    @GetMapping("/detail")
    @Parameters(value = {
        @Parameter(name = "userId", in = ParameterIn.QUERY, description = "用户ID"),
        @Parameter(name = "sex", in = ParameterIn.QUERY, description = "用户性别")
                })
    @ApiResponse(responseCode = "1000", description = "成功获取用户信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "用户不存在")
    public R<SysUserVO> detail(@RequestParam(required = true)Long userId, @RequestParam(required = false) String sex) {
        return null;
    }
}




